package com.power.using.mybomb.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.power.using.mybomb.R;
import com.power.using.mybomb.base.ImageLoaderFactory;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;

/**
 * Created by 聪明一只哈 on 2020/3/24.
 * Time: 2020/3/24  11:24
 */
public class ReceiveTextHolder extends BaseViewHolder {


    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_message)
    TextView tvMessage;

    public ReceiveTextHolder(Context context, ViewGroup root, OnRecyclerViewListener listener) {
        super(context, root, R.layout.item_chat_received_message, listener);
    }

    @OnClick(R.id.iv_avatar)
    public void onViewClicked() {
    }

    @Override
    public void bindData(Object o) {

        BmobIMMessage message= (BmobIMMessage) o;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        String time = dateFormat.format(message.getCreateTime());
        tvTime.setText(time);
        final BmobIMUserInfo info = message.getBmobIMUserInfo();
        ImageLoaderFactory.getLoader().loadAvator(ivAvatar,info != null ? info.getAvatar() : null, R.mipmap.head);
        String content =  message.getContent();
        tvMessage.setText(content);

        ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(info==null){
                    toast("由message获得的用户信息为空");
                    return;
                }
                toast("点击"+info.getName()+"的头像");
            }
        });

        tvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("点击"+message.getContent());
                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemLongClick(getAdapterPosition());
                }
            }
        });

        tvMessage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemLongClick(getAdapterPosition());
                }

                return true;
            }
        });



    }

    public void showTime(boolean isShow){
        tvTime.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

}
